<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑个人信息</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        #container {
            width: 90%;
            max-width: 1000px;
            margin: 0 auto;
            background-color: #eeeeee;
            padding: 20px;
        }

        .radio-group label {
            margin-left: 10px;
            margin-right: 30px;
        }

        form {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            margin-top: 20px;
        }

        label, input {
            margin-bottom: 10px;
            font-size: 24px;
        }

        label {
            width: 120px;
            text-align: right;
            margin-right: 20px;
        }

        input[type="radio"] {
            margin-left: 20px;
        }

        input[type="submit"] {
            width: 100%;
            height: 60px;
            line-height: 60px;
            text-align: center;
            text-decoration: none;
            color: white;
            background-color: #00adb5;
            border-radius: 10px;
            margin-top: 20px;
        }

        input[type="submit"]:hover {
            background-color: #3f72af;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        function getUserInfo() {
            // 使用jQuery的ajax方法发送GET请求
            $.ajax({
                url: "http://localhost:8080/stadium/user/inf",
                type: "GET",
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    $("#user-account").val(data.userAccount); // 将返回的数据中的userAccount字段设置为账号输入框的值
                    $("#phone").val(data.phone); // 将返回的数据中的phone字段设置为电话号码输入框的值
                    if (data.gender === "0") { // 如果返回的数据中的gender字段为male，则将男性单选按钮设置为选中状态
                        $("#male").prop("checked", true);
                    } else { // 否则将女性单选按钮设置为选中状态
                        $("#female").prop("checked", true);
                    }
                    $("#age").val(data.age); // 将返回的数据中的age字段设置为年龄输入框的值
                    $("#email").val(data.email); // 将返回的数据中的email字段设置为邮箱输入框的值
                },
                error: function (xhr) {
                    alert("获取信息失败：" + xhr.status);
                }
            });
        }

        function updateUserInfo() {
            // 使用jQuery的ajax方法发送POST请求
            $.ajax({
                url: "http://localhost:8080/stadium/common/Update",
                type: "POST",
                data: $("#update-form").serialize(),
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    alert("更新成功");
                    window.location.href = "http://localhost:8080/stadium/reservation_page/IndividualCenter.html";
                },
                error: function (xhr) {
                    alert("更新失败：" + xhr.status);
                }
            });
        }

        $(document).ready(function () {
            getUserInfo();
            $("#update-form").on("submit", function (event) {
                event.preventDefault();
                updateUserInfo();
            });
        });
    </script>
</head>
<body>
<div id="container">
    <form id="update-form">
<!--        <input type="hidden" id="userId" name="id" value="<%= Integer.parseInt(session.getAttribute("userId").toString())%>">-->
        <label for="user-account">账号：</label>
        <input type="text" id="user-account" name="userAccount" readonly>

        <label for="phone">电话：</label>
        <input type="tel" id="phone" name="phone" pattern="[0-9]{11}" required>

        <div class="radio-group">
            <label>性别：</label>
            <input type="radio" id="male" name="gender" value="male" required>
            <label for="male">男</label>
            <input type="radio" id="female" name="gender" value="female" required>
            <label for="female">女</label>
        </div>

        <label for="age">年龄：</label>
        <input type="number" id="age" name="age" min="1" max="150" required>

        <label for="email">邮箱：</label>
        <input type="email" id="email" name="email" required>

        <input type="submit" value="保存">
    </form>
</div>
</body>
</html>